import axios from 'axios';
import { Message } from 'element-ui';
import store from '../vuex'
axios.defaults.baseURL = process.env.NODE_ENV === 'production'?'':'/apis/';
export const base = '';
export const ajax={
  _fetch:ax=>{
    return new Promise((resolve, reject) => {
      axios.post(ax.url, ax.params)
        .then(response => {
          const res = response;
          if (!res.success) {
            console.log(ax); // for debug
            Message({
              message: res.msg,
              type: 'error',
              duration: 5 * 1000
            });
            reject(res);
          }
          resolve(res);
          // console.log(res);
        })
        .catch(error => {
          console.log(error); // for debug
          reject(error);
        });
    })

  },
  getBigData:ax=>{
    store.commit('commonLoading', true)
    return ajax._fetch(ax)
  },
  getMinData:ax=>{
    return ajax._fetch(ax)
  },
  postData:ax=>{
    let axs=ajax._fetch(ax)
    axs.then(data=>{
      let { msg, success} = data;
      if(success){
        Message({
          message: msg,
          type: 'success'
        });
      }
    })
    return axs
  }
}
